﻿(function ($) {

    $.fn.reName32 = function (options) {
        var defaults = {
            newName: "",
            className: "",
            focusout: function () { }
        };
        var ui32 = {
            newName: "",
            rename: function () { },
            JQtextbox: null
        };
        var options = $.extend({}, defaults, options);
        return this.each(function () {
            var JQself = $(this).hide();
            var JQtextbox = $('<input type="text" />').appendTo(JQself.parent());
            if (options.className) {
                JQtextbox.show().addClass(options.className);
            }

            JQtextbox.val(JQself.text());
            if (options.newName) {
                JQtextbox.val(options.newName);
            }

            JQtextbox.focus().select();

            ui32.JQtextbox = JQtextbox;
            ui32.rename = function () {
                JQself.text(ui32.newName);
                JQtextbox.remove();
                JQself.show();
            }

            JQtextbox.keydown(function (e) {//可以按回车来失去焦点
                if (e.keyCode == 13) {
                    JQtextbox.focusout();
                }
            });

            JQtextbox.focusout(function () {
                ui32.newName = JQtextbox.val(); //失去焦点时才是获取文本框值的时机
                JQtextbox.remove();
                JQself.show();
                if (options.focusout) {
                    options.focusout(ui32);
                }
            });
        });
    }

})(jQuery);